package com.ziyougou.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ziyougou.product.entity.AttrAttrgroupRelationEntity;
import com.ziyougou.product.entity.AttrEntity;
import com.ziyougou.product.entity.BrandEntity;
import com.ziyougou.product.service.AttrAttrgroupRelationService;
import com.ziyougou.product.service.AttrService;
import com.ziyougou.product.vo.AttrGroupAttrVo;
import com.ziyougou.product.vo.AttrGroupRelationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyougou.common.utils.PageUtils;
import com.ziyougou.common.utils.Query;

import com.ziyougou.product.dao.AttrGroupDao;
import com.ziyougou.product.entity.AttrGroupEntity;
import com.ziyougou.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public List<AttrGroupAttrVo> getAttrListByCatId(Long catId) {
        //1.在attrgroup表中根据分类id查询分组
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrGroupEntity::getCatelogId,catId);
        List<AttrGroupEntity> groupEntityList = this.baseMapper.selectList(wrapper);
        //2.查询每个组关联的属性（先查询中间表，将每个组关联的属性查询出来）
        List<AttrGroupAttrVo> attrGroupAttrVo = groupEntityList.stream().map(attrGroupEntity -> {
            AttrGroupAttrVo vo = new AttrGroupAttrVo();
            //查询出每个组对应的属性id集合
            List<Long> ids = attrAttrgroupRelationService.selectAttrIdsByGroupId(attrGroupEntity.getAttrGroupId().intValue());
            //根据属性id,查询出每个属性的值
            List<AttrEntity> attrEntityList = attrService.listByIds(ids);
            //将查询出来的分组和属性集合保存vo对象中
            BeanUtils.copyProperties(attrGroupEntity, vo);
            vo.setAttrs(attrEntityList);
            return vo;
        }).collect(Collectors.toList());

        return attrGroupAttrVo;
    }

    @Override
    public void saveRelation(List<AttrGroupRelationVO> attrGroupRelationVOList) {
        //将数据从AttrGroupRelationVO取出，复制给 AttrAttrGroupRelatioEntity
        List<AttrAttrgroupRelationEntity> entityList = attrGroupRelationVOList.stream().map(attrGroupRelationVO -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(attrGroupRelationVO, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationService.saveBatch(entityList);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //查询某个分类下的属性分组  select * from attr_group where catelog_id=xx and attr_group_name like ''
        //1.先获取模糊查条件和分类id
        String key = (String) params.get("key");
        //2.添加条件
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.like(AttrGroupEntity::getAttrGroupName,key);
        }
        String catId=(String) params.get("catId");
        if(!StringUtils.isEmpty(catId)){
            Long id=Long.parseLong(catId);
            wrapper.eq(AttrGroupEntity::getCatelogId,id);
        }
        //3.添加分页数据
        Long pageIndex= Long.parseLong((String)params.get("page"));
        Long pageSize=Long.parseLong((String)params.get("limit"));
        Page<AttrGroupEntity> p1 = new Page<>(pageIndex,pageSize);
        //4.查询数据
        IPage<AttrGroupEntity> page = baseMapper.selectPage(p1, wrapper);

        return new PageUtils(page);
    }

    @Override
    public AttrGroupEntity selectAttrGroupByCategoryId(Long catelogId) {
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrGroupEntity::getCatelogId, catelogId);
        AttrGroupEntity attrGroup = baseMapper.selectOne(wrapper);

        return attrGroup;
    }

    @Override
    public PageUtils getAttrGroupListByCatId(Map<String, Object> params, Long catId) {
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrGroupEntity::getCatelogId,catId);
        Long pageIndex= Long.parseLong((String)params.get("page"));
        Long pageSize=Long.parseLong((String)params.get("limit"));
        Page<AttrGroupEntity> p1 = new Page<>(pageIndex,pageSize);
        Page<AttrGroupEntity> page = baseMapper.selectPage(p1, wrapper);
        return  new PageUtils(page);
    }

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;
    @Override
    public List<AttrEntity> getListByGroupId(Integer attrgroupId) {
        //1.查询中间表，根据分组id,查询该分组下的属性iD列表
       List<Long> attrIds= attrAttrgroupRelationService.selectAttrIdsByGroupId(attrgroupId);
          if(attrIds!=null&&attrIds.size()>0){
              //2.根据属性id的集合，查询对应的属性值和属性名
              List<AttrEntity> attrEntityList = attrService.listByIds(attrIds);
              return attrEntityList;
          }
        return null;
    }


    @Override
    public void deleteByAttrIdAndGroupId(List<AttrGroupRelationVO> attrGroupRelationVOList) {
            //调取中间表的service层方法
         attrAttrgroupRelationService.deleteByAttrIdAndGroupId(attrGroupRelationVOList);

    }
    @Override
    public PageUtils getNoAttrListByGroupId(Integer attrgroupId, Map<String, Object> params) {
      //  1.根据分组id,查询该分组对应的分类id
        AttrGroupEntity attrGroup = this.baseMapper.selectById(attrgroupId);
        Long catelogId = attrGroup.getCatelogId();
    //    2.根据分类iD,查询属性表，得到该分类下所有的属性
        List<AttrEntity> attrEntityList=attrService.getAttrListByCatelogId(catelogId);
        List<Long> allAttrIds = attrEntityList.stream().map(attrEntity -> {
            return attrEntity.getAttrId();
        }).collect(Collectors.toList());
        //  3.根据分组id,查询该分组所关联的属性列表（attr_attrgroup_relation表）
        List<Long> attrIds = attrAttrgroupRelationService.selectAttrIdsByGroupId(attrgroupId);
        //  4.排除该分组中已经关联的属性
        allAttrIds.removeAll(attrIds);
        // 5.获取排除后的id,进行批量查询
        //分页数据
        Long pageIndex= Long.parseLong((String)params.get("page"));
        Long pageSize=Long.parseLong((String)params.get("limit"));
        Page<AttrEntity> p1 = new Page<>(pageIndex,pageSize);
        //模糊查数据
        String key = (String) params.get("key");
        LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrEntity::getAttrType,1);//只查询基本属性
        if(!StringUtils.isEmpty(key)){
            wrapper.like(AttrEntity::getAttrName,key);
        }
        if(allAttrIds.size()>0){
            wrapper.in(AttrEntity::getAttrId,allAttrIds);
        }
        //分页+模糊查
        Page<AttrEntity> page = attrService.page(p1, wrapper);
        return new PageUtils(page);
    }
}